home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / INTERVIE / GRAPHIC / CACHE.H < prev    next >
C/C++ Source or Header  |  1992-01-21  |  3KB  |  90 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Interface to associative Cache class.
  25.  */
  26.  
  27. #ifndef cache_h
  28. #define cache_h
  29.  
  30. #include <InterViews/defs.h>
  31. #include <InterViews/Graphic/ref.h>
  32.  
  33. static const UID DIRTYBITMASK = 0x80000000;
  34.  
  35. class Cache {
  36.     friend class ObjectMan;
  37. public:
  38.     Cache(int size);
  39.     ~Cache();
  40.  
  41.     boolean IsDirty(Ref ptr);        /* we don't check to make sure */
  42.     void Touch(Ref ptr);        /* ptr is really in memory, so */
  43.     void Clean(Ref ptr);        /* you'd better make sure it is! */
  44.     void TouchAll();
  45.     void CleanAll();
  46.     boolean Flush();            /* does a Save on valid pointers */
  47.  
  48.     void Set(Ref tag, Ref val);
  49.     void Unset(Ref tag);
  50.     Ref Get(Ref tag);
  51. protected:
  52.     boolean is_Dirty(Ref);
  53.     /* the value of a pointer tag contains the dirty bit */
  54.     /* (note that we don't do the same for the reverse mapping) */
  55.     void touch(Ref&);
  56.     void clean(Ref&);
  57.  
  58.     int hash(Ref tag);
  59.     /* clean and dirty tags must hash to the same value */
  60.     boolean tagsEqual(Ref, Ref);  
  61.     /* a quick & dirty check; returns false if one of the tags is */
  62.     /* is inMemory and the other isn't */
  63. protected:
  64.     int size;
  65.     class CacheEntry** hashTable;
  66. };
  67.  
  68. /*
  69.  * inlines
  70.  */
  71.  
  72. inline boolean Cache::is_Dirty (Ref r) { return (r.uid() & DIRTYBITMASK) != 0; }
  73. inline void Cache::touch (Ref& r) { r.info._uid |= DIRTYBITMASK; }
  74. inline void Cache::clean (Ref& r) { r.info._uid &= ~DIRTYBITMASK; }
  75. inline int Cache::hash (Ref tag) { clean(tag); return tag.uid() % size; }
  76.  
  77. inline boolean Cache::tagsEqual (Ref tag1, Ref tag2) {
  78.     if (tag1.inMemory() != tag2.inMemory()) {
  79.     return false;
  80.     } else if (!tag1.inMemory()) {
  81.     tag1.resetClusterBit();
  82.     tag2.resetClusterBit();
  83.     return tag1.uid() == tag2.uid();
  84.     } else {
  85.     return tag1.info.refto == tag2.info.refto;
  86.     }
  87. }
  88.  
  89. #endif
  90.